package com.hetongxue.handler;

import com.hetongxue.response.ResponseInfo;
import com.hetongxue.response.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Description: 全局异常捕获
 * @ClassNmae: GlobalExceptionHandler
 * @Author: 何同学
 * @DateTime: 2022-03-21 23:05
 **/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public Result exception(Exception e) {
        e.printStackTrace();
        log.error("发生异常...");
        return Result.Error()
                .setMessage(ResponseInfo.ERROR.getMessage())
                .setCode(ResponseInfo.ERROR.getCode());
    }

    @ExceptionHandler(RuntimeException.class)
    public Result runtimeException(RuntimeException e) {
        e.printStackTrace();
        log.error(e.getMessage());
        return Result.Error()
                .setMessage(e.getMessage());
    }

    @ExceptionHandler(NullPointerException.class)
    public Result nullPointerException(NullPointerException e) {
        e.printStackTrace();
        log.error("空指针异常");
        return Result.Error().setMessage("空指针异常").setCode(500);
    }
}
